package com.easepal802s.project.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clj.fastble.data.BleDevice;
import com.ogawa.base.Constant.BleConstant;
import com.easepal802s.project.R;
import com.easepal802s.project.interfaces.NoDoubleClickListener;
import com.easepal802s.project.observer.BleDataObserver;
import com.easepal802s.project.ui.activity.BaseMvpTestActivity;
import com.easepal802s.project.ble.BleController;
import com.example.reslib.utils.ToastUtils;

import java.lang.ref.WeakReference;

import static com.easepal802s.project.ble.BleController.CONNECT_SUCCEED;

/**
 * 普通的头部布局
 * Created by FHB on 2018/3/29.
 */

public class LayoutNormalTitle extends RelativeLayout implements View.OnClickListener, BleDataObserver.OnBleListerner {
    private ImageView mIvBack;
    private ImageView mIvPowerClose;
    private TextView mTvTitle;
    private BaseMvpTestActivity mActivity;
    private OnPowerClickListener mAction;
    private boolean isOnChange = false;//为了防止每次收到数据都设置控件
    private boolean isOffChange = false;
    private MyHandler myHandler;

    public LayoutNormalTitle(Context context) {
        super(context);
        if (context instanceof BaseMvpTestActivity) {
            mActivity = (BaseMvpTestActivity) context;
        }
    }

    public LayoutNormalTitle(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (context instanceof BaseMvpTestActivity) {
            mActivity = (BaseMvpTestActivity) context;
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        myHandler = new MyHandler(mActivity);
        BleDataObserver.getInst().registerBleDataObserver(this);
        mIvBack = findViewById(R.id.title_back);
        mIvPowerClose = findViewById(R.id.title_power_close);
        mTvTitle = findViewById(R.id.title_name);
        mIvBack.setOnClickListener(this);
        mIvPowerClose.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                Log.e("onNoDoubleClick", BleController.getInst().getConnectState() + "");
                if (BleController.getInst().getConnectState() != CONNECT_SUCCEED) {
                    if (BleController.getInst().getDevice() != null) {
                        BleController.getInst().connectDevice(BleController.getInst().getDevice());
                    } else {
                        mActivity.showBleList();
                    }

                } else {
                    if (BleController.getInst().getIsReseting()) {
                        ToastUtils.showShortToast(mActivity, R.string.is_reseting);
                    } else {
                        BleController.getInst().sendSingleData(BleConstant.TURNING, true);
                    }

                }
            }
        });
    }

    public void setTitle(String title) {
        mTvTitle.setText(title);
    }

    public void setGreenBackBotton() {
        mIvBack.setImageResource(R.drawable.auto_back_seletor);
        mIvPowerClose.setImageResource(R.drawable.auto_power_seletor);
    }

    public void setPowerOn(boolean isOn) {
        mIvPowerClose.setSelected(isOn);
    }

    public void setTitleNameGone() {
        mTvTitle.setVisibility(GONE);
    }

    public void setBackGone() {
        mIvBack.setVisibility(GONE);
    }

    public void onDestory() {
        BleDataObserver.getInst().removeBleDataObserver(this);
    }


    @Override
    public void onConnectState(int state) {
        if (state == BleController.CONNECT_FAIL) {
            myHandler.obtainMessage(2).sendToTarget();
            isOnChange = false;
            isOffChange = false;
        }
        if (state == BleController.CONNECT_BREAK) {
            myHandler.obtainMessage(2).sendToTarget();
            isOnChange = false;
            isOffChange = false;
        }
    }

    @Override
    public void onTargetDeviceFind(BleDevice bleDevice) {

    }

    @Override
    public void onStarScan() {

    }

    @Override
    public void onNoDeviceFind() {

    }

    @Override
    public void onGetData(String data) {
        if (BleController.getInst().getIsOn()) {
            if (!isOnChange) {
                myHandler.obtainMessage(0).sendToTarget();
                isOnChange = true;
                isOffChange = false;
            }
        } else {
            if (!isOffChange) {
                Log.e("SetVIEW", "ofofof");
                myHandler.obtainMessage(1).sendToTarget();
                isOnChange = false;
                isOffChange = true;
            }

        }
    }

    @Override
    public void onPowerOff() {

    }

    @Override
    public void onDataChange(String bage, int auto, int skill, int air, String name, String commond) {

    }

    @Override
    public void onShoulderAdjust(int data) {

    }

    private class MyHandler extends Handler {
        WeakReference<BaseMvpTestActivity> mMainActivityWeakReference;

        MyHandler(BaseMvpTestActivity mHomeActivity) {
            mMainActivityWeakReference = new WeakReference<>(mHomeActivity);
        }

        @Override
        public void handleMessage(Message msg) {
            final BaseMvpTestActivity mHomeActivity = mMainActivityWeakReference.get();
            if (mHomeActivity != null) {
                switch (msg.what) {
                    case 0:
                        setPowerOn(true);
                        break;
                    case 1:
                        setPowerOn(false);
                        break;
                    case 2:
                        setPowerOn(false);
                        break;


                }
            }
        }
    }

    public interface OnPowerClickListener {
        void onPowerOn(boolean isOn);
    }

    public void setListener(OnPowerClickListener action) {
        mAction = action;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_back:
                mActivity.finish();
                break;
            case R.id.title_power_close:


//                mActivity.finish();
                break;
        }
    }

}
